package cn.iocoder.yudao.module.reading.controller.app.banner;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.reading.controller.app.banner.vo.BannerRespVO;
import cn.iocoder.yudao.module.reading.convert.banner.BannerConvert;
import cn.iocoder.yudao.module.reading.dal.dataobject.banner.BannerDO;
import cn.iocoder.yudao.module.reading.service.banner.BannerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 用户 APP - 横幅管理
 *
 * @author 李晨
 */
@Tag(name = "用户 APP - 横幅管理")
@RestController
@RequestMapping("/reading/banners")
@Validated
@Slf4j
public class BannerController {

    @Resource
    private BannerService bannerService;

    @GetMapping("/active")
    @Operation(summary = "获得有效横幅列表")
    public CommonResult<List<BannerRespVO>> getActiveBanners(
            @Parameter(description = "限制数量", example = "5") @RequestParam(value = "limit", required = false) Integer limit) {
        List<BannerDO> list;
        if (limit != null && limit > 0) {
            list = bannerService.getActiveBanners(limit);
        } else {
            list = bannerService.getActiveBanners();
        }
        return success(BannerConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/click/{id}")
    @Operation(summary = "记录横幅点击")
    public CommonResult<Boolean> recordBannerClick(
            @Parameter(description = "横幅ID", required = true) @PathVariable("id") Long id) {
        bannerService.recordBannerClick(id);
        return success(true);
    }

}
